/**
 * 
 */
package nguyen.hoan.sharelocation.service;

import nguyen.hoan.sharelocation.R;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;

import com.google.android.gms.maps.model.LatLng;

/**
 * @author Hoan Apr 8, 2014 11:54:13 PM
 * 
 */
public class MyShareLocationService {

	private static final String EMPTY_STRING = "";
	private static final String COMMA = ",";
	private static final String SHARE_CHOOSE_APPLICATION = "Choose the app which you want to use for sharing the location";
	private static MyShareLocationService instance = null;

	private MyShareLocationService() {
	}

	public synchronized static MyShareLocationService getInstance() {
		if (instance == null) {
			instance = new MyShareLocationService();
		}
		return instance;
	}

	/**
	 * 
	 * @param context
	 *            must be not null.
	 */
	public void shareLocationMessage(Context context, LatLng latLng) {

		SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
		StringBuilder messageTemplate = new StringBuilder();
		messageTemplate.append(prefs.getString(context.getResources().getString(R.string.settings_message_template_key), EMPTY_STRING));
		messageTemplate.append(latLng.latitude);
		messageTemplate.append(COMMA);
		messageTemplate.append(latLng.longitude);

		Intent intent = new Intent();
		intent.setAction(Intent.ACTION_SEND);
		intent.setType("text/plain");
		intent.putExtra(Intent.EXTRA_TEXT, messageTemplate.toString());
		context.startActivity(Intent.createChooser(intent, SHARE_CHOOSE_APPLICATION));
	}
}
